package com.example.csetpp.interceptor;

import com.example.csetpp.service.impl.UniqueVisitorService;
import com.example.csetpp.utils.JwtUtil;
import com.example.csetpp.utils.UserIdHolder;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
@Slf4j
public class UniqueVisitorInterceptor implements HandlerInterceptor {

    @Autowired
    private UniqueVisitorService uniqueVisitorService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        try {
            log.info("进入UniqueVisitorInterceptor...用户ID为:{}", String.valueOf(UserIdHolder.getCurrentId()));
            uniqueVisitorService.add(String.valueOf(UserIdHolder.getCurrentId()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }
}
